



/* Complier characteristics */



/*
 How to construct complier analysis: See Bhuller et al(2020), Bald et al(2022)
*/




use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", clear 

cd  G:\Data\Workdata\707677\Common\03Output\02Regresssions



// Monotonicity

// Compliers



keep if time==1

tab obs_lawyer

egen lawyer_id_unique =group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts
bys lawyer_id_unique: egen n_lawyer=count(lawyer_id_unique)
egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique)

gen iv_judge2_all = (mean_lawyer_leniency*n_lawyer - granted)/(n_lawyer-1)

gen iv_lawyer2=iv_judge2_all-(iv_court*obs_court-granted)/(obs_court-1)




keep if n_lawyer>=20

egen n_judge=nvals(lawyer_id_unique), by(court aar)



tab n_judge

keep if n_judge>1

keep pnr iv_lawyer2 lawyer_id_unique



merge 1:m pnr using "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv"

keep if _merge==3
drop _merge


keep if time>0

tab lawyer_id_unique, gen(lawyer_dum)

egen permanent_income_groups_prior=xtile(erhvervsindk_mean), nq(2) by(pyear)




local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"





egen court_pyear =group(court pyear) 
tab court_pyear

tab time



// Judge leniency court x entry year FE


reg granted iv_lawyer2 `Xvar_short' i.court_pyear , cluster(lawyer_id_unique)

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg granted lawyer_residual  i.court_pyear , cluster(lawyer_id_unique)


gen beta_lawyer2=_b[lawyer_residual]

gen alpha_lawyer2=_b[_cons]

sort aar pnr



// Percentiles of instrument

egen p99_iv_lawyer2=pctile(iv_lawyer2), p(99)

egen p1_iv_lawyer2=pctile(iv_lawyer2), p(1)

egen p99_iv_judge2=pctile(iv_lawyer2), p(99)

egen p1_iv_judge2=pctile(iv_lawyer2), p(1)


// IV_lawyer_2

gen complier_iv_lawyer2= beta_lawyer2*(p99_iv_lawyer2-p1_iv_lawyer2) in 1 

gen always_taker_lawyer2 = alpha_lawyer2 + beta_lawyer2 * p1_iv_lawyer2 in 1

gen never_taker_lawyer2 = 1 - alpha_lawyer2 - beta_lawyer2 * p99_iv_lawyer2 in 1

gen all_lawyer2 = complier_iv_lawyer2 + always_taker_lawyer2 + never_taker_lawyer2 in 1

preserve

keep if _n==1

mkmat complier_iv_lawyer2 always_taker_lawyer2 never_taker_lawyer2, matrix(compliers) 


local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"





// Store in matrix

// put excel

putexcel set iv_compliers, replace

putexcel B1 = "complier_iv_lawyer2"
putexcel C1 = "always_taker_lawyer2"
putexcel D1 = "never_taker_lawyer2"

putexcel B2 = matrix(compliers)

restore

capture drop lawyer_residual

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg granted iv_lawyer2  i.court_pyear , cluster(lawyer_id_unique)
reg granted lawyer_residual  i.court_pyear , cluster(lawyer_id_unique)



reg erhvervsindk_real lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean
gen complier_mean = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound

// Sub-groups first-stage


// Full sample



reg granted iv_lawyer2 i.court_pyear `Xvar_short' if time==1
local individuals=e(N)	

reg granted iv_lawyer2 	 i.court_pyear `Xvar_short', cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, replace ctitle(Full sample)  keep(iv_lawyer2)  ///
 addstat(individuals, `individuals')


// Gender

reg   granted iv_lawyer2 i.court_pyear `Xvar_short' if time==1 & male==1
local individuals=e(N)	


reg granted iv_lawyer2 	 i.court_pyear `Xvar_short'  if male==1 , cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(Male)  keep(iv_lawyer2)  ///
 addstat(individuals, `individuals')
 
 
 
reg   granted iv_lawyer2 i.court_pyear `Xvar_short' if time==1 & male==0
local individuals=e(N)	
 

reg granted iv_lawyer2 	 i.court_pyear `Xvar_short'  if male==0 , cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(Female)  keep(iv_lawyer2) ///
 addstat(individuals, `individuals')


// Age


 
reg   granted iv_lawyer2 i.court_pyear `Xvar_short' if time==1 & age0<=45
local individuals=e(N)	
 

reg granted iv_lawyer2 	 i.court_pyear `Xvar_short'  if age0<=45 , cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(Young<=45)  keep(iv_lawyer2) ///
 addstat(individuals, `individuals')


 
reg   granted iv_lawyer2 i.court_pyear `Xvar_short' if time==1 & age0>45
local individuals=e(N)	 

reg granted iv_lawyer2 	 i.court_pyear `Xvar_short'  if age0>45 , cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(Old>45)  keep(iv_lawyer2) ///
 addstat(individuals, `individuals')



// Year of application


 
reg   granted iv_lawyer2 i.court_pyear `Xvar_short' if time==1 & pyear<=1994
local individuals=e(N)	 


reg granted iv_lawyer2 	 i.court_pyear `Xvar_short'  if pyear<=1994 , cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(Pyear<=1994)  keep(iv_lawyer2) ///
 addstat(individuals, `individuals')


reg   granted iv_lawyer2 i.court_pyear `Xvar_short' if time==1 & pyear>1994
local individuals=e(N)	  
 
reg granted iv_lawyer2 	 i.court_pyear `Xvar_short'  if pyear>1994 , cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(Pyear>1994)  keep(iv_lawyer2) ///
 addstat(individuals, `individuals')


// Education: 9-klasse vs else:


reg   granted iv_lawyer2 i.court_pyear `Xvar_short' if time==1 & lower_secondary==1
local individuals=e(N)	 

reg granted iv_lawyer2 	 i.court_pyear `Xvar_short'  if lower_secondary==1 , cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(low-education)  keep(iv_lawyer2) ///
 addstat(individuals, `individuals')


reg   granted iv_lawyer2 i.court_pyear `Xvar_short' if time==1 & lower_secondary==0
local individuals=e(N)	 
 

reg granted iv_lawyer2 	 i.court_pyear `Xvar_short'  if lower_secondary==0, cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(High education)  keep(iv_lawyer2) ///
 addstat(individuals, `individuals')

 // Marriage status legmarried_m1
 
 reg   granted iv_lawyer2 i.court_pyear `Xvar_short' if time==1 & legmarried_m1==1
local individuals=e(N)	 

reg granted iv_lawyer2 	 i.court_pyear `Xvar_short'  if legmarried_m1==1 , cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(married)  keep(iv_lawyer2) ///
 addstat(individuals, `individuals')
 
  
 reg   granted iv_lawyer2 i.court_pyear `Xvar_short' if time==1 & legmarried_m1==0
local individuals=e(N)	 

reg granted iv_lawyer2 	 i.court_pyear `Xvar_short'  if legmarried_m1==0 , cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(not-married)  keep(iv_lawyer2) ///
 addstat(individuals, `individuals')
 
// Permanent income 
	

	
 reg   granted iv_lawyer2 i.court_pyear `Xvar_short' if time==1 & permanent_income_groups_prior==2
local individuals=e(N)	 

reg granted iv_lawyer2 	 i.court_pyear `Xvar_short'  if permanent_income_groups_prior==2 , cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(high-income)  keep(iv_lawyer2) ///
 addstat(individuals, `individuals')
 
  
 reg   granted iv_lawyer2 i.court_pyear `Xvar_short' if time==1 & permanent_income_groups_prior==1
local individuals=e(N)	 

reg granted iv_lawyer2 	 i.court_pyear `Xvar_short'  if permanent_income_groups_prior==1 , cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(low-income)  keep(iv_lawyer2) ///
 addstat(individuals, `individuals')	

// employment status




////////////////////
// Reverse sample //
////////////////////




// Gender 




use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", clear 

cd  G:\Data\Workdata\707677\Common\01Data\02STATA




keep if time==1

tab obs_lawyer

egen lawyer_id_unique=group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts

keep age0 lawyer_id_unique granted male schooling court pyear pnr lawyerid

bys lawyer_id_unique male: egen n_lawyer=count(lawyer_id_unique)

egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique male)
egen mean_court_leniency=mean(granted), by(court male)


gen mean_lawyer_leniency_male=.
replace mean_lawyer_leniency_male= mean_lawyer_leniency-mean_court_leniency if male==1

gen n_lawyer_male=.
replace n_lawyer_male=n_lawyer if male==1

gen n_lawyer_female=.
replace n_lawyer_female=n_lawyer if male==0

gen mean_lawyer_leniency_female=.
replace mean_lawyer_leniency_female= mean_lawyer_leniency - mean_court_leniency if male==0

duplicates drop lawyer_id_unique male, force

preserve 

keep lawyer_id_unique mean_lawyer_leniency_male n_lawyer_male male lawyerid court

keep if male==1

save male, replace

restore


keep lawyer_id_unique mean_lawyer_leniency_female n_lawyer_female male lawyerid court

keep if male==0

merge 1:1 lawyer_id_unique using male


keep if _merge==3


keep  lawyer_id_unique mean_lawyer_leniency_female n_lawyer_female lawyerid court mean_lawyer_leniency_male n_lawyer_male

save male, replace


// age




use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", clear 


keep if time==1

// Age dummy age0>45=1 age0<=45=0
gen age_old=.
replace age_old=1 if age0>45
replace age_old=0 if age0<=45

egen lawyer_id_unique=group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts

keep age0 lawyer_id_unique granted male schooling court pyear pnr lawyerid age_old

bys lawyer_id_unique age_old: egen n_lawyer=count(lawyer_id_unique)

egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique age_old)
egen mean_court_leniency=mean(granted), by(court age_old)


gen mean_lawyer_leniency_old=.
replace mean_lawyer_leniency_old= mean_lawyer_leniency-mean_court_leniency if age_old==1

gen n_lawyer_old=.
replace n_lawyer_old=n_lawyer if age_old==1

gen n_lawyer_young=.
replace n_lawyer_young=n_lawyer if age_old==0

gen mean_lawyer_leniency_young=.
replace mean_lawyer_leniency_young= mean_lawyer_leniency - mean_court_leniency if age_old==0

duplicates drop lawyer_id_unique age_old, force

preserve 

keep lawyer_id_unique mean_lawyer_leniency_old n_lawyer_old age_old lawyerid court

keep if age_old==1

save age, replace

restore


keep lawyer_id_unique mean_lawyer_leniency_young n_lawyer_young age_old lawyerid court

keep if age_old==0

merge 1:1 lawyer_id_unique using age


keep if _merge==3


keep  lawyer_id_unique mean_lawyer_leniency_young n_lawyer_young lawyerid court mean_lawyer_leniency_old n_lawyer_old age_old

save age, replace




// Education


use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", clear 


keep if time==1

// education dummy  if lower_secondary==1(folkeskole) 

// Education edu_type_m1

gen education_long=.
replace education_long=1 if lower_secondary==0 
replace education_long=0 if lower_secondary==1 

egen lawyer_id_unique=group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts

keep age0 lawyer_id_unique granted male court pyear pnr lawyerid schooling education_long

bys lawyer_id_unique education_long: egen n_lawyer=count(lawyer_id_unique)

egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique education_long)
egen mean_court_leniency=mean(granted), by(court education_long)


gen mean_lawyer_leniency_edu_long=.
replace mean_lawyer_leniency_edu_long= mean_lawyer_leniency-mean_court_leniency if education_long==1

gen n_lawyer_edu_long=.
replace n_lawyer_edu_long=n_lawyer if education_long==1

gen n_lawyer_edu_short=.
replace n_lawyer_edu_short=n_lawyer if education_long==0

gen mean_lawyer_leniency_edu_short=.
replace mean_lawyer_leniency_edu_short= mean_lawyer_leniency - mean_court_leniency if education_long==0

duplicates drop lawyer_id_unique education_long, force

preserve 

keep lawyer_id_unique mean_lawyer_leniency_edu_long n_lawyer_edu_long education_long lawyerid court

keep if education_long==1

save education, replace

restore


keep lawyer_id_unique mean_lawyer_leniency_edu_short n_lawyer_edu_short education_long lawyerid court

keep if education_long==0

merge 1:1 lawyer_id_unique using education


keep if _merge==3


keep  lawyer_id_unique mean_lawyer_leniency_edu_short n_lawyer_edu_short lawyerid court mean_lawyer_leniency_edu_long n_lawyer_edu_long education_long

save education, replace



// Median income



use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", clear 


keep if time==1


	egen permanent_income_groups_prior=xtile(erhvervsindk_mean), nq(2) by(pyear)
	
egen lawyer_id_unique=group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts

keep age0 lawyer_id_unique granted male court pyear pnr lawyerid schooling  permanent_income_groups_prior

bys lawyer_id_unique permanent_income_groups_prior: egen n_lawyer=count(lawyer_id_unique)

egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique permanent_income_groups_prior)
egen mean_court_leniency=mean(granted), by(court permanent_income_groups_prior)


gen mean_lawyer_leniency_high_income=.
replace mean_lawyer_leniency_high_income= mean_lawyer_leniency-mean_court_leniency if permanent_income_groups_prior==2

gen n_lawyer_high_income=.
replace n_lawyer_high_income=n_lawyer if permanent_income_groups_prior==2

gen n_lawyer_low_income=.
replace n_lawyer_low_income=n_lawyer if permanent_income_groups_prior==1

gen mean_lawyer_leniency_low_income=.
replace mean_lawyer_leniency_low_income= mean_lawyer_leniency - mean_court_leniency if permanent_income_groups_prior==1

duplicates drop lawyer_id_unique permanent_income_groups_prior, force

preserve 

keep lawyer_id_unique lawyerid court mean_lawyer_leniency_high_income n_lawyer_high_income permanent_income_groups_prior

keep if permanent_income_groups_prior==2

save income, replace

restore



keep lawyer_id_unique lawyerid court mean_lawyer_leniency_low_income n_lawyer_low_income permanent_income_groups_prior

keep if permanent_income_groups_prior==1

merge 1:1 lawyer_id_unique using income


keep if _merge==3


keep  lawyer_id_unique lawyerid court mean_lawyer_leniency_high_income n_lawyer_high_income  mean_lawyer_leniency_low_income n_lawyer_low_income

save income, replace



// First stage regression using split sample


use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", clear 

cd  G:\Data\Workdata\707677\Common\03Output\02Regresssions



merge m:1 lawyerid court using "G:\Data\Workdata\707677\Common\01Data\02STATA\male"



drop _merge



merge m:1 lawyerid court using "G:\Data\Workdata\707677\Common\01Data\02STATA\education"


drop _merge



merge m:1 lawyerid court using "G:\Data\Workdata\707677\Common\01Data\02STATA\age"

drop _merge 



merge m:1 lawyerid court using "G:\Data\Workdata\707677\Common\01Data\02STATA\income"



keep if time>0

tab lawyer_id_unique, gen(lawyer_dum)


	egen permanent_income_groups_prior=xtile(erhvervsindk_mean), nq(2) by(pyear)

	

local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



egen court_pyear =group(court pyear) 
tab court_pyear

tab time


local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"


// Gender


reg granted mean_lawyer_leniency_female i.court_pyear `Xvar_short'  if male==1 & n_lawyer_female>=10 & time==1
local individuals=e(N)	

reg granted mean_lawyer_leniency_female i.court_pyear `Xvar_short'  if male==1 & n_lawyer_female>=10 , cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(Male reverse sample)  keep(mean_lawyer_leniency_female) ///
 addstat(individuals, `individuals')


reg granted mean_lawyer_leniency_male i.court_pyear `Xvar_short'  if male==0 & n_lawyer_male>=10 & time==1
local individuals=e(N) 
 
 
reg granted mean_lawyer_leniency_male i.court_pyear `Xvar_short'  if male==0 & n_lawyer_male>=10, cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(Female reverse sample) keep(mean_lawyer_leniency_male) addstat(individuals, `individuals')

// Age

reg granted mean_lawyer_leniency_old  i.court_pyear `Xvar_short'  if age0<=45 & n_lawyer_old>=10 & time==1
local individuals=e(N)

reg granted mean_lawyer_leniency_old  i.court_pyear `Xvar_short'  if age0<=45 & n_lawyer_old>=10, cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(Young<=45 reverse sample)  keep( mean_lawyer_leniency_old) ///
 addstat(individuals, `individuals')

reg granted mean_lawyer_leniency_young i.court_pyear `Xvar_short'  if age0>45 & n_lawyer_young>=10 & time==1
local individuals=e(N)

 
reg granted mean_lawyer_leniency_young i.court_pyear `Xvar_short'  if age0>45 & n_lawyer_young>=10, cluster(lawyer_id_unique)

outreg2 using iv_firststage_subsample.xls, append ctitle(Old>45 reverse sample)  keep(mean_lawyer_leniency_young) ///
 addstat(individuals, `individuals')


// Education: 9-klasse vs else:
reg granted mean_lawyer_leniency_edu_long i.court_pyear `Xvar_short'  if lower_secondary==1 & n_lawyer_edu_long>=10 & time==1
local individuals=e(N)

reg granted mean_lawyer_leniency_edu_long i.court_pyear `Xvar_short'  if lower_secondary==1 & n_lawyer_edu_long>=10, cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(low-education reverse sample)  keep(mean_lawyer_leniency_edu_long) ///
 addstat(individuals, `individuals')


reg granted mean_lawyer_leniency_edu_short i.court_pyear `Xvar_short'  if lower_secondary==0 & n_lawyer_edu_short>=10 & time==1 
local individuals=e(N)
 
reg granted mean_lawyer_leniency_edu_short i.court_pyear `Xvar_short'  if lower_secondary==0 & n_lawyer_edu_short>=10, cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(High education reverse sample)  keep(mean_lawyer_leniency_edu_short) ///
 addstat(individuals, `individuals')

// High income vs low income. permanent_income_groups_prior

// Low income

reg granted mean_lawyer_leniency_high_income i.court_pyear `Xvar_short'  if permanent_income_groups_prior==1 & n_lawyer_high_income>=10 & time==1
local individuals=e(N)

reg granted mean_lawyer_leniency_high_income i.court_pyear `Xvar_short'  if permanent_income_groups_prior==1 & n_lawyer_high_income>=10, cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(low-income reverse sample)  keep(mean_lawyer_leniency_high_income) ///
 addstat(individuals, `individuals')


// High income


reg granted mean_lawyer_leniency_low_income i.court_pyear `Xvar_short'  if permanent_income_groups_prior==2 & n_lawyer_low_income>=10 & time==1
local individuals=e(N)

reg granted mean_lawyer_leniency_low_income i.court_pyear `Xvar_short'  if permanent_income_groups_prior==2 & n_lawyer_low_income>=10, cluster(lawyer_id_unique)
outreg2 using iv_firststage_subsample.xls, append ctitle(high-income reverse sample)  keep(mean_lawyer_leniency_low_income) ///
 addstat(individuals, `individuals')

 
 
